package com.xuhao.didi.oksocket.btnview.utils;

import ohos.agp.components.Attr;
import ohos.agp.components.AttrSet;
import ohos.agp.utils.Color;
import ohos.hiviewdfx.HiLog;
import ohos.hiviewdfx.HiLogLabel;
import java.util.Locale;
import java.util.NoSuchElementException;
/*
 * Copyright (C) 2021 Huawei Device Co., Ltd.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

public class AttrUtils {
    static final HiLogLabel label = new HiLogLabel(HiLog.LOG_APP, 0x12345, "yuki");

    /**
     * string类型
     *
     * @param attrs 获取
     * @param attrName 值
     * @param defValue 默认
     * @return string值
     */
    public static String getString(AttrSet attrs, String attrName, String defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getStringValue();
        }
    }

    /**
     * 转成int类型
     *
     * @param attrs 值
     * @param attrName 值
     * @param defValue 默认
     * @return int值
     */
    public static int getInt(AttrSet attrs, String attrName, int defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getIntegerValue();
        }
    }

    /**
     * float
     *
     * @param attrs 获取
     * @param attrName 值
     * @param defValue 默认
     * @return float值
     */
    public static float getFloat(AttrSet attrs, String attrName, float defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            return attr.getFloatValue();
        }
    }

    private static Attr attrNoSuchElement(AttrSet attrs, String attrName) {
        Attr attr = null;
        try {
            attr = attrs.getAttr(attrName).get();
        } catch (NoSuchElementException e) {
            HiLog.info(label, "Exception = " + e.toString());
        }
        return attr;
    }

    /**
     * string 颜色值 转换
     *
     * @param attrs
     * @param attrName
     * @param defValue
     * @return 颜色
     */
    public static Color getStringToColor(AttrSet attrs, String attrName, Color defValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return defValue;
        } else {
            String strColor = attr.getStringValue();
            return new Color(Color.getIntColor(strColor));
        }
    }

    /**
     * bool类型
     *
     * @param attrs
     * @param attrName
     * @param isDefValue
     * @return true 符合反之
     */
    public static boolean getStringToBool(AttrSet attrs, String attrName, boolean isDefValue) {
        Attr attr = attrNoSuchElement(attrs, attrName);
        if (attr == null) {
            return isDefValue;
        } else {
            String mStringBool = attr.getStringValue();
            return mStringBool.toLowerCase(Locale.ROOT).equals("true");
        }
    }
}
